]> git.r.bdr.sh - rbdr/captura/blobdiff - Captura/Core Extensions/CVImageBuffer+cgImage.swift
Add sparkle
[rbdr/captura] / Captura / Core Extensions / CVImageBuffer+cgImage.swift
index c30cb0c9433f4858e78d919fed16f8d0ed3d74d1..3f59b8ec1006a023fed8dcdd31167672521e8ff7 100644 (file)
@@ -3,9 +3,19 @@ import ReplayKit
 
 extension CVImageBuffer {
   
+  private static let contextQueue = DispatchQueue(label: "com.example.contextQueue")
+  static let sharedContext: CIContext = {
+          return CIContext()
+  }()
+  
   var cgImage: CGImage? {
-    let ciImage = CIImage(cvImageBuffer: self)
-    let context = CIContext()
-    return context.createCGImage(ciImage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(self), height: CVPixelBufferGetHeight(self)))
+    var result: CGImage?
+    CVImageBuffer.contextQueue.sync {
+      let ciImage = CIImage(cvImageBuffer: self)
+      let width = CVPixelBufferGetWidth(self)
+      let height = CVPixelBufferGetHeight(self)
+      result = CVImageBuffer.sharedContext.createCGImage(ciImage, from: CGRect(x: 0, y: 0, width: width, height: height))
+    }
+    return result
   }
 }